package com.game.character;

import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;

import com.game.bullet.BulletManager;
import com.game.character.TypeCharacter.TypeObject;
import com.game.collision.CollisionCheck;
import com.game.framwork.gamesupport.AngleCal;
import com.game.framwork.gamesupport.GameSprite;
import com.game.framwork.itf.Game;
import com.game.framwork.itf.Graphics;
import com.game.framwork.itf.Input.TouchEvent;
import com.game_ltdd.Resrouce;

public class Player extends Character {

	private float timeWaiting;

	private float dieTime;
	private float maxDieTime;

	private boolean isMovePress;
	private boolean isMoveDragged;
	private Rect moveControlBound;
	private Rect fireControlBound;

	private int angle;

	public Player(Game game) {
		type = TypeObject.Player;
		timeDelayShot = 0;
		timeMaxDelayShot = 0.3f;
		dieTime = 0.0f;
		maxDieTime = 2.0f;
		this.runSpeed = 5.0f;
		moveControlBound = new Rect(0, game.getGraphics().getHeight() - 125,
				125, game.getGraphics().getHeight());
		fireControlBound = new Rect(game.getGraphics().getWidth() - 100, game
				.getGraphics().getHeight() - 100,
				game.getGraphics().getWidth(), game.getGraphics().getHeight());
		init();
	}

	private void init() {
		this.objectImage = Resrouce.player;
		GameSprite[] pAnim;
		pAnim = new GameSprite[11];

		pAnim[0] = new GameSprite(this.objectImage);
		pAnim[0].createAnimation("Stand", 0, 0, 0, 1.0f);
		pAnim[1] = new GameSprite(this.objectImage);
		pAnim[1].createAnimation("Stand Animation", 1, 2, 1, 1.0f / 4.0f);
		pAnim[2] = new GameSprite(this.objectImage);
		pAnim[2].createAnimation("Move Left", 3, 7, 3, 1.0f / 10.0f);
		pAnim[3] = new GameSprite(this.objectImage);
		pAnim[3].createAnimation("Move Right", 8, 12, 8, 1.0f / 10.0f);
		pAnim[4] = new GameSprite(this.objectImage);
		pAnim[4].createAnimation("Move Top", 13, 17, 13, 1.0f / 10.0f);
		pAnim[5] = new GameSprite(this.objectImage);
		pAnim[5].createAnimation("Move Bot", 18, 22, 18, 1.0f / 10.0f);
		pAnim[6] = new GameSprite(this.objectImage);
		pAnim[6].createAnimation("Die", 23, 24, 24, 1.0f / 5.0f);
		pAnim[7] = new GameSprite(this.objectImage);
		pAnim[7].createAnimation("Stand Left", 3, 3, 3, 1.0f / 10.0f);
		pAnim[8] = new GameSprite(this.objectImage);
		pAnim[8].createAnimation("Stand Right", 8, 8, 8, 1.0f / 10.0f);
		pAnim[9] = new GameSprite(this.objectImage);
		pAnim[9].createAnimation("Stand Top", 13, 13, 13, 1.0f / 10.0f);
		pAnim[10] = new GameSprite(this.objectImage);
		pAnim[10].createAnimation("Stand Bot", 18, 18, 18, 1.0f / 10.0f);

		loadSprite(this.objectImage, 23, 10, 66, 82, pAnim);
		createBoundBox(2, (82 - 40) / 4 - 5, 30, 40);
		currState = STAND;
	}

	@Override
	public void updateInternal(float deltaTime) {
		if (!isDeath && !isDying) {
			timeDelayShot += deltaTime;
			if (isMovePress || isMoveDragged) {
				move(currentSpeed);
			}
			CollisionCheck.collisionOnMap(this, tiledMap);
			CollisionCheck.collisionWithBullet(this,
					BulletManager.getInstance().listBullet);
			if (currState == STAND) {
				timeWaiting += deltaTime;
				if (timeWaiting >= 1.0f) {
					timeWaiting = 0;
					changeAnimation("Stand Animation");
				}
			} else {
				timeWaiting = 0;
			}
		} else {
			dying(deltaTime);
		}
		super.updateInternal(deltaTime);
	}

	private void dying(float deltaTime) {
		changeAnimation("Die");
		dieTime += deltaTime;
		if (dieTime >= maxDieTime) {
			dieTime = 0.0f;
			isDeath = true;
		}
	}

	@Override
	public void paintInternal(Graphics g) {
		g.drawRect(moveControlBound.left, moveControlBound.top,
				moveControlBound.width(), moveControlBound.height(),
				Color.YELLOW);
		g.drawRect(fireControlBound.left, fireControlBound.top,
				fireControlBound.width(), fireControlBound.height(),
				Color.YELLOW);
		super.paintInternal(g);
	}

	@Override
	public void move(float speed) {
		currentSpeed = speed;
		super.move(speed);
	}

	private int touchDownX;
	private int touchDownY;

	public void touchUpHandle(TouchEvent event, float deltaTime) {
		int x = event.x;
		int y = event.y;
		isMovePress = false;
		isMoveDragged = false;
		if (moveControlBound.contains(x, y)) {
			findStandState(currState);
		}
		return;
	}

	public void touchDraggedHandle(TouchEvent event, float deltaTime) {
		int x = event.x;
		int y = event.y;
		angle = AngleCal.getAlphaByTwoPoint(new Point(touchDownX, touchDownY),
				new Point(x, y));
		if (moveControlBound.contains(x, y)) {
			isMoveDragged = true;
			if (angle >= 45 && angle < 135) {
				if (currState != MOVE_TOP) {
					currState = MOVE_TOP;
					changeAnimation("Move Top");
				}
				currentSpeed = -runSpeed;
				return;
			}
			if (angle >= 135 && angle < 225) {
				if (currState != MOVE_LEFT) {
					currState = MOVE_LEFT;
					changeAnimation("Move Left");
				}
				currentSpeed = -runSpeed;
				return;
			}
			if (angle >= 225 && angle < 315) {
				if (currState != MOVE_BOTTOM) {
					currState = MOVE_BOTTOM;
					changeAnimation("Move Bot");
				}
				currentSpeed = runSpeed;
				return;
			}
			if (angle >= 315 || angle < 45) {
				if (currState != MOVE_RIGHT) {
					currState = MOVE_RIGHT;
					changeAnimation("Move Right");
				}
				currentSpeed = runSpeed;
				return;
			}

		}
	}

	public void touchDownHandle(TouchEvent event, float deltaTime) {
		touchDownX = moveControlBound.centerX();
		touchDownY = moveControlBound.centerY();
		int x = event.x;
		int y = event.y;
		angle = AngleCal.getAlphaByTwoPoint(new Point(touchDownX, touchDownY),
				new Point(x, y));
		if (fireControlBound.contains(x, y)) {
			if (timeDelayShot >= timeMaxDelayShot) {
				BulletManager.getInstance().createBulletOfPlayer(this);
				timeDelayShot = 0f;
			}
		}

		if (moveControlBound.contains(x, y)) {
			isMovePress = true;
			if (angle >= 135 && angle < 225) {
				currState = MOVE_LEFT;
				changeAnimation("Move Left");
				currentSpeed = -runSpeed;
				return;
			}
			if (angle >= 45 && angle < 135) {
				currState = MOVE_TOP;
				changeAnimation("Move Top");
				currentSpeed = -runSpeed;
				return;
			}
			if (angle >= 225 && angle < 315) {
				currState = MOVE_BOTTOM;
				changeAnimation("Move Bot");
				currentSpeed = runSpeed;
				return;
			}
			if (angle >= 315 || angle < 45) {
				currState = MOVE_RIGHT;
				changeAnimation("Move Right");
				currentSpeed = runSpeed;
				return;
			}
		}
	}
}
